import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/widgets/btn_dropdown.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class WidgetBetRecordDropdownGame extends StatelessWidget {
  const WidgetBetRecordDropdownGame({
    super.key,
    required this.teamRows,
    required this.teamRx,
    required this.statusRows,
    required this.timeRows,
    required this.timeRx,
    required this.statusRx,
    required this.gameRows,
    required this.gameRx,
    required this.editingController,
    required this.onRequestRefresh,
    required this.onTimeSelection,
    this.userEditingController,
  });

  final List<String> teamRows;
  final List<String> gameRows;
  final List<String> statusRows;
  final List<String> timeRows;

  final RxString teamRx;
  final RxString gameRx;
  final RxString statusRx;
  final RxString timeRx;

  final Function(String) onTimeSelection;
  final Function(String) onRequestRefresh;

  final TextEditingController? userEditingController;
  final TextEditingController editingController;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(16.w).copyWith(bottom: 24.w),
      decoration: BoxDecoration(
        color: AppColor.navBgColor,
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(30.w),
          bottomRight: Radius.circular(30.w),
        ),
      ),
      child: Obx(() {
        return Column(
          children: [
            _buildTopDropdown(teamRows,
                value: teamRx.value,
                width: 1.sw - 32.w,
                btnColor: AppColor.primary, onChanged: (value) {
              teamRx.value = value;
            }),
            4.verticalSpaceFromWidth,
            Row(
              children: [
                Expanded(
                    child: _buildTopDropdown(gameRows, value: gameRx.value,
                        onChanged: (value) {
                  gameRx.value = value;
                })),
                4.horizontalSpace,
                Expanded(
                    child: _buildTopDropdown(statusRows, value: statusRx.value,
                        onChanged: (value) {
                  statusRx.value = value;
                })),
                4.horizontalSpace,
              ],
            ),
            4.verticalSpaceFromWidth,
            Row(
              children: [
                Expanded(
                    child: _buildTopDropdown(timeRows,
                        btnColor: AppColor.primary,
                        value: timeRx.value, onChanged: (value) {
                  // logic.updateTimeSelection(value);
                  onTimeSelection.call(value);
                  // updateTimeSelection
                })),
                if (userEditingController != null)
                  Expanded(
                    child: _buildInput(
                            userEditingController!, LocaleKeys.memberId.tr)
                        .marginOnly(left: 4.w),
                  ),
                4.horizontalSpace,
                Expanded(
                  child: _buildInput(
                      editingController, LocaleKeys.betOrderNumber.tr),
                )
              ],
            )
          ],
        );
      }),
    );
  }

  Widget _buildInput(TextEditingController controller, String hintText) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 6.w, vertical: 6.5.w)
          .copyWith(left: 12.w),
      decoration: BoxDecoration(
          border: Border(
              bottom: BorderSide(
                  color: AppColor.textPrimary.withOpacity(0.15), width: 1)),
          color: Colors.black.withOpacity(0.3),
          borderRadius: BorderRadius.circular(8.w)),
      child: Row(
        children: [
          Expanded(
              child: TextField(
            controller: controller,
            // maxLength: 30,

            style: TextStyle(
              fontSize: 14.sp,
              fontWeight: FontWeight.w500,
              color: AppColor.textPrimary.withOpacity(0.94),
            ),
            decoration: InputDecoration(
              border: InputBorder.none,
              enabledBorder: InputBorder.none,
              focusedBorder: InputBorder.none,
              disabledBorder: InputBorder.none,
              errorBorder: InputBorder.none,
              focusedErrorBorder: InputBorder.none,
              counterText: '',
              hintText: hintText,
              isDense: true,
              // 紧凑布局，有助于垂直对齐
              hintStyle: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w400,
                color: AppColor.textPrimary.withOpacity(0.5),
              ),
              contentPadding: EdgeInsets.zero, // 调整内边距
            ),
            onSubmitted: (text) {
              onRequestRefresh.call(text);
              // logic.refreshController.requestRefresh();
            },
          )),
          GestureDetector(
            onTap: () {
              onRequestRefresh.call(controller.text);
              // logic.refreshController.requestRefresh();
            },
            child: AutoImage(
              Assets.commonSearchBtn,
              width: 32.w,
            ),
          )
        ],
      ),
    );
  }

  Widget _buildTopDropdown(List<String> items,
      {required String value,
      required Function(String) onChanged,
      MainAxisAlignment? mainAxisAlignment,
      double? width,
      Color? btnColor}) {
    return BtnDropdown(
        items: items,
        value: value,
        width: width,
        // width: 1.sw - 32.w,
        mainAxisAlignment: mainAxisAlignment,
        onChanged: (value) {
          onChanged.call(value);
        },
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 12.w),
          decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(
                      color: AppColor.textPrimary.withOpacity(0.15), width: 1)),
              color: Colors.black.withOpacity(0.3),
              borderRadius: BorderRadius.circular(8.w)),
          child: Row(
            children: [
              Expanded(
                  child: Text(
                value,
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w900,
                  color: btnColor ?? AppColor.textPrimary.withOpacity(0.94),
                ),
              )),
              AutoImage(
                Assets.promotionCommissionCaretDown,
                width: 16.w,
              )
            ],
          ),
        ));
  }
}
